package com.eblacorp.archive.security.ldap;

import javax.naming.NamingException;

import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;

import com.eblacorp.archive.security.ldap.model.Contact;
/**
 * @author Mahmoud.Ezzat
 * 
 */
public class ContactAttributeMapper implements ContextMapper<Contact> {

	@Override
	public Contact mapFromContext(Object ctx) throws NamingException {
		DirContextOperations dirContext = (DirContextOperations) ctx;
		DistinguishedName dn = new DistinguishedName(dirContext.getDn());
		String fullDn;
		fullDn = dirContext.getNameInNamespace();

		Contact person = new Contact();
		person.setDn(fullDn);
		person.setPrimaryKey(dirContext.getDn().toString());
		person.setCountry(dn.getLdapRdn(0).getComponent().getValue());
		person.setCompany(dn.getLdapRdn(1).getComponent().getValue());
		person.setFullName(dirContext.getStringAttribute("cn"));
		person.setLastName(dirContext.getStringAttribute("sn"));
		person.setDescription(dirContext.getStringAttributes("description"));
		person.setPhone(dirContext.getStringAttribute("telephoneNumber"));
		person.setPassword(dirContext.getStringAttribute("unicodepwd"));
		
		return person;

	}

}